'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
	let {name_keys, coid_keys,ps_keys,mast_keys,factor_keys,ifactor_keys} = event;
	var ret = [];
	
	if (name_keys != undefined) {
	  const retPromises = name_keys.map(key => {
		return uniCloud.callFunction({
		  name: "getInfo",
		  data: {
			name_key: key
		  }
		}).then(res => {
		  return res.result.data;
		});
	  });

	  return Promise.all(retPromises).then(results => {
		const firstResult = results[0]; // 获取第一个结果数组
		const intersection = firstResult.filter(obj1 => {
		  return results.every(result => {
			return result.some(obj2 => obj2._id === obj1._id); // 根据 _id 属性进行比较
		  });
		});
		return intersection;
	  });
	}
	else if (ps_keys != undefined) {
		  const retPromises = ps_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				ps_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
		const firstResult = results[0]; // 获取第一个结果数组
		const intersection = firstResult.filter(obj1 => {
		  return results.every(result => {
			return result.some(obj2 => obj2._id === obj1._id); // 根据 _id 属性进行比较
		  });
		});
		return intersection;
	  });
	}
	else if (mast_keys != undefined) {
		  const retPromises = mast_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				mast_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
		const firstResult = results[0]; // 获取第一个结果数组
		const intersection = firstResult.filter(obj1 => {
		  return results.every(result => {
			return result.some(obj2 => obj2._id === obj1._id); // 根据 _id 属性进行比较
		  });
		});
		return intersection;
	  });
	}
	else if (coid_keys != undefined) {
		  const retPromises = coid_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				coid_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
		const firstResult = results[0]; // 获取第一个结果数组
		const intersection = firstResult.filter(obj1 => {
		  return results.every(result => {
			return result.some(obj2 => obj2._id === obj1._id); // 根据 _id 属性进行比较
		  });
		});
		return intersection;
	  });
	}
	else if (factor_keys != undefined) {
		  const retPromises = factor_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				factor_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
		const firstResult = results[0]; // 获取第一个结果数组
		const intersection = firstResult.filter(obj1 => {
		  return results.every(result => {
			return result.some(obj2 => obj2._id === obj1._id); // 根据 _id 属性进行比较
		  });
		});
		return intersection;
	  });
	}
	else if (ifactor_keys != undefined) {
		  const retPromises = ifactor_keys.map(key => {
			return uniCloud.callFunction({
			  name: "getInfo",
			  data: {
				ifactor_key: key
			  }
			}).then(res => {
			  return res.result.data;
			});
		  });
	return Promise.all(retPromises).then(results => {
		const firstResult = results[0]; // 获取第一个结果数组
		const intersection = firstResult.filter(obj1 => {
		  return results.every(result => {
			return result.some(obj2 => obj2._id === obj1._id); // 根据 _id 属性进行比较
		  });
		});
		return intersection;
	  });
	}

};

